package com.sinodata.bsm.cicp.finder.tlq;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.finder.Finder;
import com.sinodata.bsm.cicp.util.CommandReader;

/**
 * 
 * <p>
 * Description: TongLink 的发现程序， 发现 指定 tlqname 的 业务码
 * </p>
 *
 * @author liulibo
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-12-26 AM 10:24:14   liulibo        1.0         To create
 * </p>
 *
 * @since 
 * @see
 */
public class TLQFinder extends Finder {

    private static final Logger logger = Logger.getLogger(TLQFinder.class);

    /**
     * 发现程序
     * @param pResValue  父资源的信息
     * @param pattrVals  父资源属性
     * @param attributes 资源自带的属性
     * @return 资源列表
     */
    /*@Override
    public List<List<Serializable>> find(ResValue pResValue, ResAttributeValValue[] pattrVals, ResAttributeValue[] attributes) {
        String shell = "tlqstat -lZ 1";
        BufferedReader reader = null;
        try {
            reader = CollectorUtil.getReader(shell, host);
            return this.parseReader(reader, pattrVals, attributes);
        } catch (IOException e) {
            logger.error("Failed to auto find TLQ ", e);
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    logger.error("Failed to close IO resource ", e);
                }
            }
        }
        return new ArrayList<List<Serializable>>();
    }*/

    /**
     * 解析命令响应结果
     * 
     * @param reader 命令响应结果
     * @param pattrVals  接口父资源 节点的属性信息
     * @param attributes 资源自带的属性
     * @return 资源列表
     * @throws IOException
     */

    /*private List<List<Serializable>> parseReader(BufferedReader reader, ResAttributeValValue[] pattrVals, ResAttributeValue[] attributes) throws IOException {
        List<List<Serializable>> tlqResources = null;
        Pattern typePatn = Pattern.compile(".*\\[(\\S+)\\]\\s*");
        Pattern codepatn = Pattern.compile(".*\\[(\\S+)\\].*\\-{5,}+.*");
        String tmp = null;
        List<Serializable> resAndAttr = null;
        Matcher match = null;
        while ((tmp = reader.readLine()) != null) {
            match = typePatn.matcher(tmp);
            if (tmp.indexOf("<Self>:") > -1 || tmp.indexOf("<To Down>:") > -1) {// 说明信息，跳过
                continue;
            } else if (tmp.indexOf("------") > -1 && tlqResources != null) {
                resAndAttr = new ArrayList<Serializable>();
                String busCode = getCode(codepatn, tmp);
                resAndAttr.add(findRes(busCode));
                resAndAttr.add(findAttr(attributes, busCode));
                tlqResources.add(resAndAttr);
            } else if (match.matches()) {// 匹配指定tlqname 的数据才是目标数据
                if (pattrVals[0].getAttrValue().indexOf(match.group(1)) > -1) {
                    tlqResources = new ArrayList<List<Serializable>>();
                }
            }
        }
        return tlqResources;
    }*/

    /**
     * 匹配正则表达式，并 捕获组
     * 
     * @param codePatn 正则模板
     * @param tmp 目标字符串
     * @return 捕获的组的值
     */
    private String getCode(Pattern codePatn, String tmp) {
        Matcher codeMach = codePatn.matcher(tmp);
        codeMach.matches();
        return codeMach.group(1);
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<String[]> find(String ip, Map params) {
        String shell = "tlqstat -lZ 1";
        BufferedReader reader = null;
        try {
            reader = CommandReader.getReader(ip, shell, 0, 0);
            return parseReader(reader, params);
        } catch (IOException e) {
            logger.error("Failed to auto find TLQ ", e);
            return new ArrayList<String[]>();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    logger.error("Failed to close IO resource ", e);
                }
            }
        }
    }

    @SuppressWarnings("unchecked")
    private List<String[]> parseReader(BufferedReader reader, Map params) throws IOException {
        List<String[]> list = null;
        Pattern typePatn = Pattern.compile(".*\\[(\\S+)\\]\\s*");
        Pattern codepatn = Pattern.compile(".*\\[(\\S+)\\].*\\-{5,}+.*");
        String tmp = null;
        Matcher match = null;
        String tlqname = (String) params.get("TLQNAME");
        while ((tmp = reader.readLine()) != null) {
            match = typePatn.matcher(tmp);
            if (tmp.indexOf("<Self>:") > -1 || tmp.indexOf("<To Down>:") > -1) {// 说明信息，跳过
                continue;
            } else if (tmp.indexOf("------") > -1 && list != null) {
                String busCode = getCode(codepatn, tmp);
                String[] info = new String[4];
                list.add(info);
                info[0] = busCode;
                info[1] = busCode;
                info[2] = "MQ connect CICS business code";
                info[3] = busCode;
            } else if (match.matches()) {// 匹配指定tlqname 的数据才是目标数据
                if (tlqname.indexOf(match.group(1)) > -1) {
                    list = new ArrayList<String[]>();
                }
            }
        }
        return list;
    }

}
